package wang.unclecat.databinding.slice;

import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.ComponentContainer;
import ohos.mp.metadata.binding.dao.SimpleDao;
import ohos.mp.metadata.binding.databinding.DataSourceConnectionException;
import ohos.mp.metadata.binding.databinding.MetaDataRequestInfo;
import ohos.mp.metadata.binding.metadata.MetaData;
import ohos.utils.PacMap;
import timber.log.Timber;
import wang.unclecat.databinding.ResourceTable;
import wang.unclecat.databinding.metadatabinding.SlicehelloMetaDataBinding;

import java.util.Arrays;

public class HelloSlice extends AbilitySlice {

    private int count = 0;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_slice_hello);

        SimpleDao simpleDao = SimpleDao.create(new HelloDataMetaDataHandler());


        SimpleDao simpleDaoUi = SimpleDao.create(new HelloUiDataHandler());


        SlicehelloMetaDataBinding binding = new SlicehelloMetaDataBinding();



        MetaDataRequestInfo request = new MetaDataRequestInfo.Builder()
                .fromRequestItems(binding.getRequestMetaData())
                .setSyncRequest("data", true)
                .setSyncRequest("helloUi", true)
                .setCustomDao("data", simpleDao)
                .setCustomDao("helloUi", simpleDaoUi)
                .build();

        try {
            binding = SlicehelloMetaDataBinding.requestBinding(this, request, null);
        } catch (DataSourceConnectionException e) {
            e.printStackTrace();
        }

//        // getting binding layout component.
        ComponentContainer container = (ComponentContainer) binding.getLayoutComponent();
        setUIContent(container);

        container.findComponentById(ResourceTable.Id_text_helloworld)
                .setClickedListener(component -> {
                    count++;
                    simpleDao.update("msg", count+"");
//                    simpleDao.notifyChange(simpleDao.getMetaData(),"msg", count);
                    simpleDaoUi.update("count", count);
                });
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }


    private static class HelloUiDataHandler implements SimpleDao.ISimpleMetaDataHandler {

        @Override
        public boolean onConnect(SimpleDao simpleDao) {
            return true;
        }

        @Override
        public void onQuery(SimpleDao simpleDao, MetaData metaData, String[] strings, PacMap pacMap) {
            Timber.d("onQuery() called with: simpleDao = [ %s ], \nmetaData = [ %s ], \nstrings = [ %s ], \npacMap = [ %s ]\n", simpleDao, metaData, Arrays.toString(strings), pacMap);
            metaData.put("count", 0);//初始化
        }

        @Override
        public void onDelete() {

        }

        //触发方式 simpleDao.notifyChange(simpleDao.getMetaData(),"count", count);
        @Override
        public void onChange(String s, Object o) {
            Timber.d("onChange() called with: s = [ %s ], o = [ %s ]", s, o);
            //更新数据到存储介质
        }

        //触发方式 simpleDao.notifyChange(simpleDao.getMetaData(),null, null);
        @Override
        public void onFlush() {
            Timber.d("onFlush() called");
            //更新全部数据到存储介质
        }

        @Override
        public void onDisconnect(SimpleDao simpleDao) {

        }
    }

    public class HelloDataMetaDataHandler implements SimpleDao.ISimpleMetaDataHandler {

        @Override
        public boolean onConnect(SimpleDao simpleDao) {
            return true;
        }

        @Override
        public void onQuery(SimpleDao simpleDao, MetaData metaData, String[] strings, PacMap pacMap) {
            Timber.d("onQuery() called with: simpleDao = [ %s ], \nmetaData = [ %s ], \nstrings = [ %s ], \npacMap = [ %s ]\n", simpleDao, metaData, Arrays.toString(strings), pacMap);
            metaData.put("msg", 100+"");
        }

        @Override
        public void onDelete() {

        }

        @Override
        public void onChange(String s, Object o) {
        }

        @Override
        public void onFlush() {
        }

        @Override
        public void onDisconnect(SimpleDao simpleDao) {

        }
    }
}
